package com.example.onlinemusic.controller;

import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.mapper.MusicMapper;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 均先生
 * Date: 2024-02-28
 * Time: 14:46
 */
@RestController
@RequestMapping("/loveMusic")
public class LoveMusicController {
    @Autowired
    private LoveMusicMapper loveMusicMapper;
    @Autowired
    private MusicMapper musicMapper;


    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> insertLoveMusic(@RequestParam String id, HttpServletRequest request) {


        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录");
            return new ResponseBodyMessage<>(-1,"没有登录",false);
        }

        int musicId = Integer.parseInt(id);
        System.out.println("musicId" + musicId);
        Music tmp = musicMapper.findMusicByMusicId(musicId);
        if (tmp == null) {
            return new ResponseBodyMessage<>(-1,"音乐不存在",false);
        }

        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId" + userId);
        Music music = loveMusicMapper.findLoveMusicByUserIdAndMusicId(userId,musicId);
        if (music != null) {//TODO:收藏过就不能再收藏
            return new ResponseBodyMessage<>(-1,"你之前收藏过不能收藏了",false);
        }

        Boolean flg = loveMusicMapper.insertLoveMusic(userId,musicId);
        if (flg) {
            return new ResponseBodyMessage<>(1,"收藏成功!",true);
        }

        return new ResponseBodyMessage<>(-1,"收藏失败",false);
    }

    @RequestMapping("/dislikeMusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id, HttpServletRequest request) {
        int musicId = Integer.parseInt(id);
        System.out.println("musicId" + musicId);

        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录");
            return new ResponseBodyMessage<>(-1,"没有登录",false);
        }
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId" + userId);

        Music music = loveMusicMapper.findLoveMusicByUserIdAndMusicId(userId,musicId);
        if (music == null) {//TODO:没有收藏过就不能取消收藏
            return new ResponseBodyMessage<>(-1,"你没有收藏过不能取消收藏",false);
        }

        boolean flg = loveMusicMapper.deleteLoveMusic(userId,musicId);
        if (flg) {
            return new ResponseBodyMessage<>(1,"取消收藏成功!",true);
        }

        return new ResponseBodyMessage<>(-1,"取消收藏失败",false);
    }

    @RequestMapping("/findLoveMusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,
                                                          HttpServletRequest request) {
        //1.登录检查
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录");
            return new ResponseBodyMessage<>(-1,"没有登录",null);
        }

        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        List<Music> musicList = null;
        if (musicName != null) {
            musicList = loveMusicMapper.findLoveMusicByKeyAndUID(musicName,userId);
        }else {
            musicList = loveMusicMapper.findLoveMusicByUserId(userId);
        }
        return new ResponseBodyMessage<>(1,"查询到了所有的音乐",musicList);

    }
}
